package cn.kgc.tangcco.tcbd1011.no13.user.action;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.kgc.tangcco.tcbd1011.no13.base.action.BaseServlet;
import cn.kgc.tangcco.tcbd1011.no13.base.properties.factory.PropertiesFactory;
import cn.kgc.tangcco.tcbd1011.no13.base.util.Utils;
import cn.kgc.tangcco.tcbd1011.no13.user.entity.User;
import cn.kgc.tangcco.tcbd1011.no13.user.service.UserService;
@WebServlet(name="DoLoginServlet",urlPatterns="/doLogin.action")
public class DoLoginServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	private UserService uservice;
	public DoLoginServlet() {
		uservice=(UserService) PropertiesFactory.getInstance(Utils.USERSERVICE);
	}
	public String login(HttpServletRequest request, HttpServletResponse response) {
		String forward = "";
		String requestCode = request.getParameter("requestCode");
		HttpSession session  = request.getSession();
		String sessionCode = (String) session.getAttribute("vcode");
		if(requestCode!=null) {
			if(requestCode.equals(sessionCode)) {
				User login=uservice.login(new User(request.getParameter("loginuserName"), request.getParameter("loginpassword")));
				if(login!=null) {
					forward="main/main";
				}else {
					request.setAttribute("msg", "对不起，用户名或者密码错误！");
					forward="login";
				}
			}else {
					request.setAttribute("msg", "对不起验证码错误");
					forward = "login";
			}
			
		
		}
		
		return forward;
	}
	public String toLogin(HttpServletRequest request, HttpServletResponse response) {
		String forward = "login";
		return forward;
	}
}
